home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / directx / dxf / samples / multimedia / directshow / common / dshowutil.cpp next >
Encoding:
C/C++ Source or Header  |  2000-10-19  |  6.0 KB  |  241 lines

  1. //------------------------------------------------------------------------------
  2. // File: DShowUtil.cpp
  3. //
  4. // Desc: DirectShow sample code - utility functions.
  5. //
  6. // Copyright (c) 1996 - 2000, Microsoft Corporation.  All rights reserved.
  7. //------------------------------------------------------------------------------
  8.  
  9.  
  10. #include "dshowutil.h"
  11.  
  12.  
  13. HRESULT GetPin( IBaseFilter * pFilter, PIN_DIRECTION dirrequired, int iNum, IPin **ppPin)
  14. {
  15.     CComPtr< IEnumPins > pEnum;
  16.     *ppPin = NULL;
  17.     HRESULT hr = pFilter->EnumPins(&pEnum);
  18.     if(FAILED(hr)) 
  19.         return hr;
  20.  
  21.     ULONG ulFound;
  22.     IPin *pPin;
  23.     hr = E_FAIL;
  24.     while(S_OK == pEnum->Next(1, &pPin, &ulFound))
  25.     {
  26.         PIN_DIRECTION pindir = (PIN_DIRECTION)3;
  27.         pPin->QueryDirection(&pindir);
  28.         if(pindir == dirrequired)
  29.         {
  30.             if(iNum == 0)
  31.             {
  32.                 *ppPin = pPin;
  33.                 break;
  34.             }
  35.             iNum--;
  36.         } // if
  37.         pPin->Release();
  38.     } // while
  39.  
  40.     return hr;
  41. }
  42.  
  43.  
  44. IPin * GetInPin( IBaseFilter * pFilter, int Num )
  45. {
  46.     CComPtr< IPin > pComPin;
  47.     GetPin(pFilter, PINDIR_INPUT, Num, &pComPin);
  48.     return pComPin;
  49. }
  50.  
  51.  
  52. IPin * GetOutPin( IBaseFilter * pFilter, int Num )
  53. {
  54.     CComPtr< IPin > pComPin;
  55.     GetPin(pFilter, PINDIR_OUTPUT, Num, &pComPin);
  56.     return pComPin;
  57. }
  58.  
  59.  
  60. HRESULT FindOtherSplitterPin(IPin *pPinIn, GUID guid, int nStream, IPin **ppSplitPin)
  61. {
  62.     DbgLog((LOG_TRACE,1,TEXT("FindOtherSplitterPin")));
  63.  
  64.     CheckPointer(ppSplitPin, E_POINTER);
  65.     CComPtr< IPin > pPinOut;
  66.     pPinOut = pPinIn;
  67.  
  68.     while(pPinOut)
  69.     {
  70.         PIN_INFO ThisPinInfo;
  71.         pPinOut->QueryPinInfo(&ThisPinInfo);
  72.         if(ThisPinInfo.pFilter) ThisPinInfo.pFilter->Release();
  73.  
  74.         pPinOut = NULL;
  75.         CComPtr< IEnumPins > pEnumPins;
  76.         ThisPinInfo.pFilter->EnumPins(&pEnumPins);
  77.         if(!pEnumPins)
  78.         {
  79.             return NULL;
  80.         }
  81.  
  82.         // look at every pin on the current filter...
  83.         //
  84.         ULONG Fetched = 0;
  85.         while(1)
  86.         {
  87.             CComPtr< IPin > pPin;
  88.             Fetched = 0;
  89.             ASSERT(!pPin); // is it out of scope?
  90.             pEnumPins->Next(1, &pPin, &Fetched);
  91.             if(!Fetched)
  92.             {
  93.                 break;
  94.             }
  95.  
  96.             PIN_INFO pi;
  97.             pPin->QueryPinInfo(&pi);
  98.             if(pi.pFilter) pi.pFilter->Release();
  99.  
  100.             // if it's an input pin...
  101.             //
  102.             if(pi.dir == PINDIR_INPUT)
  103.             {
  104.                 // continue searching upstream from this pin
  105.                 //
  106.                 pPin->ConnectedTo(&pPinOut);
  107.  
  108.                 // a pin that supports the required media type is the
  109.                 // splitter pin we are looking for!  We are done
  110.                 //
  111.             }
  112.             else
  113.             {
  114.                 CComPtr< IEnumMediaTypes > pMediaEnum;
  115.                 pPin->EnumMediaTypes(&pMediaEnum);
  116.                 if(pMediaEnum)
  117.                 {
  118.                     Fetched = 0;
  119.                     AM_MEDIA_TYPE *pMediaType;
  120.                     pMediaEnum->Next(1, &pMediaType, &Fetched);
  121.                     if(Fetched)
  122.                     {
  123.                         if(pMediaType->majortype == guid)
  124.                         {
  125.                             if(nStream-- == 0)
  126.                             {
  127.                                 DeleteMediaType(pMediaType);
  128.                                 *ppSplitPin = pPin;
  129.                                 (*ppSplitPin)->AddRef();
  130.                                 DbgLog((LOG_TRACE,1,TEXT("Found SPLIT pin")));
  131.                                 return S_OK;
  132.                             }
  133.                         }
  134.                         DeleteMediaType(pMediaType);
  135.                     }
  136.                 }
  137.             }
  138.  
  139.             // go try the next pin
  140.  
  141.         } // while
  142.     }
  143.     ASSERT(FALSE);
  144.     return E_FAIL;
  145. }
  146.  
  147.  
  148. HRESULT SeekNextFrame( IMediaSeeking * pSeeking, double FPS, long Frame )
  149. {
  150.     // try seeking by frames first
  151.     //
  152.     HRESULT hr = pSeeking->SetTimeFormat(&TIME_FORMAT_FRAME);
  153.     REFERENCE_TIME Pos = 0;
  154.     if(!FAILED(hr))
  155.     {
  156.         pSeeking->GetCurrentPosition(&Pos);
  157.         Pos++;
  158.     }
  159.     else
  160.     {
  161.         // couldn't seek by frames, use Frame and FPS to calculate time
  162.         //
  163.         Pos = REFERENCE_TIME(double( Frame * UNITS ) / FPS);
  164.  
  165.         // add a half-frame to seek to middle of the frame
  166.         //
  167.         Pos += REFERENCE_TIME(double( UNITS ) * 0.5 / FPS);
  168.     }
  169.  
  170.     hr = pSeeking->SetPositions(&Pos, AM_SEEKING_AbsolutePositioning, 
  171.                                 NULL, AM_SEEKING_NoPositioning);
  172.     return hr;
  173.  
  174. }
  175.  
  176. #ifdef DEBUG
  177.     // for debugging purposes
  178.     const INT iMAXLEVELS = 5;                // Maximum debug categories
  179.     extern DWORD m_Levels[iMAXLEVELS];       // Debug level per category
  180. #endif
  181.  
  182.  
  183. void TurnOnDebugDllDebugging( )
  184. {
  185. #ifdef DEBUG
  186.     for(int i = 0 ; i < iMAXLEVELS ; i++)
  187.     {
  188.         m_Levels[i] = 1;
  189.     }
  190. #endif
  191. }
  192.  
  193.  
  194. void DbgPrint( char * pText )
  195. {
  196.     DbgLog(( LOG_TRACE, 1, "%s", pText ));
  197. }
  198.  
  199. void ErrPrint( char * pText )
  200. {
  201.     printf(pText);
  202.     return;
  203. }
  204.  
  205.  
  206. HRESULT AddGraphToRot(IUnknown *pUnkGraph, DWORD *pdwRegister) 
  207. {
  208.     IMoniker * pMoniker;
  209.     IRunningObjectTable *pROT;
  210.     WCHAR wsz[128];
  211.     HRESULT hr;
  212.  
  213.     if (FAILED(GetRunningObjectTable(0, &pROT))) {
  214.         return E_FAIL;
  215.     }
  216.  
  217.     wsprintfW(wsz, L"FilterGraph %08x pid %08x", (DWORD_PTR)pUnkGraph, 
  218.               GetCurrentProcessId());
  219.  
  220.     hr = CreateItemMoniker(L"!", wsz, &pMoniker);
  221.     if (SUCCEEDED(hr)) {
  222.         hr = pROT->Register(0, pUnkGraph, pMoniker, pdwRegister);
  223.         pMoniker->Release();
  224.     }
  225.     pROT->Release();
  226.     return hr;
  227. }
  228.  
  229.  
  230. void RemoveGraphFromRot(DWORD pdwRegister)
  231. {
  232.     IRunningObjectTable *pROT;
  233.  
  234.     if (SUCCEEDED(GetRunningObjectTable(0, &pROT))) {
  235.         pROT->Revoke(pdwRegister);
  236.         pROT->Release();
  237.     }
  238. }
  239.  
  240.  
  241.